<template>
  <div>
    <el-dialog v-model="dialogFormVisible" :title="title" destroy-on-close>
      <el-form ref="ruleFormRef" :model="form" label-width="150" style="padding: 0px 15px" :rules="rule" status-icon dis>
        <el-form-item :label="name.enterpriseCode" :label-width="formLabelWidth" prop="enterpriseCode">
          <el-input clearable placeholder="请输入配置项编码" v-model="form.enterpriseCode" />
        </el-form-item>
        <el-form-item prop="collectitemName" :label="name.collectitemName" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入采集数据项指标名称" v-model="form.collectitemName" />
        </el-form-item>
        <el-form-item prop="dataCode" :label="name.dataCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入采集配置项编码" v-model="form.dataCode" />
        </el-form-item>
        <el-form-item prop="processCode" :label="name.processCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入生产工序" v-model="form.processCode" />
        </el-form-item>
        <el-form-item prop="processUnitCode" :label="name.processUnitCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入工序单元" v-model="form.processUnitCode" />
        </el-form-item>
        <el-form-item prop="equipmentCode" :label="name.equipmentCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入重点设备类型" v-model="form.equipmentCode" />
        </el-form-item>
        <el-form-item prop="equipmentUnitCode" :label="name.equipmentUnitCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入重点能耗设备编码" v-model="form.equipmentUnitCode" />
        </el-form-item>
        <!-- <el-form-item prop="equipmentUnitCode" :label="name.equipmentUnitCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入重点能耗设备编码" v-model="form.equipmentUnitCode" />
        </el-form-item> -->
        <el-form-item prop="energyClassCode" :label="name.energyClassCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入采集对象类型" v-model="form.energyClassCode" />
        </el-form-item>
        <el-form-item prop="energyTypeCode" :label="name.energyTypeCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入能源分类 + 分项" v-model="form.energyTypeCode" />
        </el-form-item>
        <el-form-item prop="dataUsageCode" :label="name.dataUsageCode" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入用途编码" v-model="form.dataUsageCode" />
        </el-form-item>
        <el-form-item prop="inputType" :label="name.inputType" :label-width="formLabelWidth">
          <!-- <el-input clearable placeholder="请输入数据采集来源" v-model="form.inputType" /> -->
          <el-select v-model="form.inputType" placeholder="请选择数据采集来源">
            <el-option key="1" label="管理信息系统" value="1" />
            <el-option key="2" label="生产监控管理系统" value="2" />
            <el-option key="3" label="分布式控制系统" value="3" />
            <el-option key="4" label="现场仪表" value="4" />
            <el-option key="5" label="手工填报" value="5" />
            <el-option key="6" label="能源供应单位" value="6" />
            <el-option key="7" label="其他" value="7" />
          </el-select>
        </el-form-item>
        <el-form-item prop="statType" :label="name.statType" :label-width="formLabelWidth">
          <!-- <el-input clearable placeholder="请输入采集频率" v-model="form.statType" /> -->
          <el-select v-model="form.statType" placeholder="请选择采集频率">
            <el-option key="1" label="实时" value="0" />
            <el-option key="2" label="日" value="1" />
            <el-option key="3" label="月" value="2" />
            <el-option key="4" label="年" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item prop="regVersion" :label="name.regVersion" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入平台服务版本" v-model="form.regVersion" />
        </el-form-item>
        <el-form-item prop="dicVersion" :label="name.dicVersion" :label-width="formLabelWidth">
          <el-input clearable placeholder="请输入基础数据版本" v-model="form.dicVersion" />
        </el-form-item>
        <el-form-item style="width: 100%; margin-left: 0px">
          <div>
            <el-button @click="dialogFormVisible = false" type="info">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确定
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import mitts from "@/utils/bus.js";
import { datacollectconfigureAdd, datacollectconfigureUpdateById } from "@/api/enterprise";
import { ElMessage } from "element-plus";

const dialogFormVisible = ref(false);
const title = ref("新增");
const ruleFormRef = ref();
const isEdit = ref(false);
let form = reactive({
  dataId: "", //主键
  enterpriseCode: "", //统一社会信用代码
  collectitemName: "", //采集数据项指标名称
  dataCode: "", //采集数据项编码
  processCode: "", //生产工序
  processUnitCode: "", //工序单元
  equipmentCode: "", //重点设备类型
  equipmentUnitCode: "", //重点能耗设备编码
  energyClassCode: "", //采集对象类型
  energyTypeCode: "", //能源分类 + 分项
  dataUsageCode: "", //用途编码
  inputType: "", //数据采集来源
  statType: "", //采集频率
  regVersion: "", //平台服务版本
  dicVersion: "", //基础数据版本
});
const name = {
  dataId: "主键", //主键
  enterpriseCode: "统一社会信用代码:", //统一社会信用代码
  collectitemName: "采集数据项指标名称:", //采集数据项指标名称
  dataCode: "采集数据项编码:", //采集数据项编码
  processCode: "生产工序:", //生产工序
  processUnitCode: "工序单元:", //工序单元
  equipmentCode: "重点设备类型:", //重点设备类型
  equipmentUnitCode: "重点能耗设备编码:", //重点能耗设备编码
  energyClassCode: "采集对象类型:", //采集对象类型
  energyTypeCode: "能源分类 + 分项:", //能源分类 + 分项
  dataUsageCode: "用途编码:", //用途编码
  inputType: "数据采集来源:", //数据采集来源
  statType: "采集频率:", //采集频率
  regVersion: "平台服务版本:", //平台服务版本
  dicVersion: "基础数据版本:", //基础数据版本
};
let rule = {};

//接收到是否打开的参数
mitts.on("visible", (e) => {
  dialogFormVisible.value = e;
});
mitts.on("add", (e) => {
  if (e == true) {
    form = reactive({
      dataId: "", //主键
      enterpriseCode: "", //统一社会信用代码
      collectitemName: "", //采集数据项指标名称
      dataCode: "", //采集数据项编码
      processCode: "", //生产工序
      processUnitCode: "", //工序单元
      equipmentCode: "", //重点设备类型
      equipmentUnitCode: "", //重点能耗设备编码
      energyClassCode: "", //采集对象类型
      energyTypeCode: "", //能源分类 + 分项
      dataUsageCode: "", //用途编码
      inputType: "", //数据采集来源
      statType: "", //采集频率
      regVersion: "", //平台服务版本
      dicVersion: "", //基础数据版本
    });
    isEdit.value = false;
  }
});
//接收到实体请求
mitts.on("enterprise", (e) => {
  title.value = "编辑";
  form = reactive(e);
  isEdit.value = true;
});
const doDatalist = () => {
  for (let key in name) {
    let tempd = JSON.parse(
      '[{"required":true,"message":"请输入数据","trigger":"blur"}]'
    );
    if (key == 'dataCode') {
      tempd = JSON.parse(
        '[{"required":true,"message":"请输入数据","trigger":"blur"},{ "min": 16, "max": 16, "message": "请输入16位采集数据项编码", "trigger": "blur" }]'
      )
    }
    rule[key] = tempd;
  }
};
const submitForm = async (formEl) => {
  await formEl.validate((valid, fields) => {
    if (valid) {
      //验证成功,开始进行数据的更新或者新增
      if (isEdit.value == false) {
        datacollectconfigureAdd(form)
          .then((res) => {
            if (res.status == 200) {
              ElMessage.success("上传数据成功！");
              dialogFormVisible.value = false;
              mitts.emit("isUpload", true);
            } else {
              ElMessage.warning("服务器异常,请稍后再试！");
            }
          })
          .catch((msg) => { });
        return;
      }
      datacollectconfigureUpdateById(form)
        .then((res) => {
          if (res.status == 200) {
            ElMessage.success(res.message);
            dialogFormVisible.value = false;
            mitts.emit("isUpload", true);
          } else {
            ElMessage.warning("服务器异常,请稍后再试！");
          }
        })
        .catch((msg) => { });
    } else {
      console.log("error submit!", fields);
    }
  });
};
doDatalist();
</script>

<style lang="less" scoped></style>
